package socket

import (
	"bytes"
	"net"
)

type UdpWrap struct {
	conn       *net.UDPConn
	remoteAddr *net.UDPAddr
	readerBuf  *bytes.Reader
}

func newUdpWrap(conn *net.UDPConn, remoteAddr *net.UDPAddr, buff []byte) *UdpWrap {
	return &UdpWrap{conn: conn, remoteAddr: remoteAddr, readerBuf: bytes.NewReader(buff)}
}

func (u *UdpWrap) Conn() *net.UDPConn {
	return u.conn
}

func (u *UdpWrap) RemoteAddr() net.Addr {
	return u.remoteAddr
}

func (u *UdpWrap) Read(p []byte) (n int, err error) {
	return u.readerBuf.Read(p)
}

func (u *UdpWrap) Write(p []byte) (n int, err error) {
	return u.conn.WriteTo(p, u.remoteAddr)
}

func (u *UdpWrap) WriteAddr(p []byte, addr *net.UDPAddr) (n int, err error) {
	return u.conn.WriteToUDP(p, addr)
}
